package com.mwx.baselib.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import me.yokeyword.fragmentation.anim.DefaultNoAnimator;

/**
 * Fragment父类
 */
public abstract class BaseFragment<B extends ViewDataBinding> extends SupportFragment {
    private static final String TAG = BaseFragment.class.getSimpleName();
    protected B binding;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = null;
        if (getLayout() instanceof Integer) {
            binding = DataBindingUtil.inflate(inflater, (Integer) getLayout(), container, false);
            view = binding.getRoot();

        } else if (getLayout() instanceof View) {
            view = (View) getLayout();
            binding = DataBindingUtil.findBinding(view);

        }
        setFragmentAnimator(new DefaultNoAnimator());
        init(savedInstanceState);
        return view;
    }
    protected abstract Object getLayout();
    protected abstract void init(Bundle savedInstanceState);
    protected abstract void initData();

    protected abstract void initView();

    protected abstract void initListener();



    /**
     * intent跳转
     * @param clazz
     */
    protected void openActivity( Class<? extends BaseActivity> clazz){
        Intent intent = new Intent(_mActivity,clazz);
        _mActivity.startActivity(intent);
    }    /**
     * intent带值跳转
     * @param clazz
     * @param bundle
     */
    protected void openActivity(Class<? extends BaseActivity> clazz, Bundle bundle){
        Intent intent = new Intent(_mActivity,clazz);
        intent.putExtras(bundle);
        _mActivity.startActivity(intent);
    }
    /**
     * 带返回值的跳转
     * @param clazz
     * @param bundle
     * @param requestCode
     */
    protected void openActivity(Class<? extends BaseActivity> clazz, Bundle bundle, int requestCode){
        Intent intent = new Intent(_mActivity,clazz);
        intent.putExtras(bundle);
        getActivity().startActivityForResult(intent,requestCode);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
